<?php
/**
 * 二维码控制器         
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class QrcodesController extends AdmincommonController {
    /**
     * 显示商品列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $filters = array();
        if (!empty($_POST["codeType"]) && $_POST["status"] != 2) {
            $filters['codeType'] = intval(remove_xss($_POST['codeType']));
        }
        $qrcodes = D("Qrcodes");
        $_GET['p'] = $_POST['p'];
        $count = $qrcodes->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $qrcodes->limit($Page->firstRow.','.$Page->listRows)
                          ->relation(true)
                         ->where($filters)
                         ->select();
        $this->assign('list',$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }

    /**
     * 查看二维码
     * @param HttpRequest $request
     * @return void
     * @author Antonia
     */
    public function Qrcodes_info() {
        $id = intval($_GET['id']);
        $re = array();
        if (empty($id)) {
            $re['statusCode'] = "300";
            $re['message'] = "该二维码不存在";
            echo json_encode($re);
            exit;
        }
        $info = D("Qrcodes")->find($id);
        if ($info) {
            $this->assign("info",$info);
            $this->display("info");
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "该二维码不存在！";
            echo json_encode($re);
        }
    }

    /**
     * 下载二维码
     * @param HttpRequest $request
     * @return void
     * @author Antonia
     */
    public function Qrcodes_download() {
        $id = intval($_GET['id']);
        $re = array();
        if (empty($id)) {
            $re['statusCode'] = "300";
            $re['message'] = "该二维码不存在";
            echo json_encode($re);
            exit;
        }
        $info = D("Qrcodes")->find($id);
        if ($info) {     
            $url=$info["codeUrl"];
            $file =$_SERVER['DOCUMENT_ROOT'].$url;
            if(file_exists($file)){
                $filename= basename($url);
                header("Content-type: image/pjpeg/png");
                header("Content-Disposition: attachment; filename=".$filename);
                readfile($file);
            }else{
                echo "不存在";
            }
            exit();      
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "该二维码不存在！";
            echo json_encode($re);
        }
    }

    /**
     * 改变状态
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update_status() {
        $re = array();
        if (!empty($_GET['id'])) {
            $qrcodes = D("Qrcodes");
            $result = $qrcodes->find(remove_xss($_GET['id']));
            if ($result["status"] == 1) {
                $data["status"] = 0;
            }else{
                $data["status"] = 1;
            }
            $data["id"]= $result["id"];
            $rs = $qrcodes->save($data);
            if (false !== $rs) {
                $re['statusCode'] = "200";
                $re['message'] = "操作成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "操作失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

}
?>